var express = require('express');
var router = express.Router();

// add module
var fs = require('fs');
var path = require('path');
var crypto = require('crypto');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});


/* GET update_info. */

function getFileMD5(filePath) {
    var buffer = fs.readFileSync(filePath);
    var fsHash = crypto.createHash('md5');

    fsHash.update(buffer);
    var md5 = fsHash.digest('hex');
    console.log("文件的MD5是：%s", md5);

    return md5;
}

router.get('/update_info', function(req, res, next) {
    var device = req.query.device;
    console.log('current device:' + device);
    var info = [
        {
            'url': '/ota_file/update_v1.zip',
            'updateMessage': 'Fix bugs.',
            'versionName': 'v1',
            'md5': '35b9547c461545382be9809a2e99559a'
        },
        {
            'url': '/ota_file/update_v2.zip',
            'updateMessage': 'Fix bugs.',
            'versionName': 'v2',
            'md5': '35b9547c461545382be9809a2e99559a'
        },
    ];
    res.writeHead(200, {'Content-Type': 'application/json;charset=utf-8'});
    res.end(JSON.stringify(info));

    //var info = {
    //    'url': '/ota_file/update_v1.zip',
    //    'updateMessage': 'Fix bugs.',
    //    'versionName': 'v1',
    //    'md5': '35b9547c461545382be9809a2e99559a'
    //};
    //var dir = process.cwd() + '/ota_file'
    //var filePath = path.join(dir, 'update.zip');
    //var md5 = getFileMD5(filePath);
    //info.md5 = md5;
    //res.writeHead(200, {'Content-Type': 'application/json;charset=utf-8'});
    //res.end(JSON.stringify(info));
});

/* GET Download File path. */
router.get('/ota_file/:filename', function(req, res, next) {
    var filename = req.params.filename;
    var dir = process.cwd() + '/ota_file'
    var filePath = path.join(dir, filename);

    fs.exists(filePath, function(exist) {
        if (exist) {
            console.log('downloading:' + filename);
            res.download(filePath);
        }
        else {
            res.set('Content-type', 'text/html');
            res.end('File not exist.');
        }
    });
});

module.exports = router;
